package io.zim.modules.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.zim.common.utils.CharUtil;
import io.zim.common.utils.PageUtils;
import io.zim.common.utils.Query;
import io.zim.common.utils.R;
import io.zim.common.validator.ValidatorUtils;
import io.zim.common.validator.group.AddGroup;
import io.zim.common.validator.group.UpdateGroup;
import io.zim.modules.back.entity.SysApplyEntity;
import io.zim.modules.back.service.SysApplyService;
import io.zim.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 应用信息接口
 *
 * @author zzh
 * @email 862583291@qq.com
 * @date 2019年05月13日 上午15:28:55
 */
@RestController
@RequestMapping("/back/apply")
public class SysApplyController extends AbstractController {
  @Autowired
  private SysApplyService sysApplyService;

  /**
   * 查看列表
   */
  @RequestMapping("/list")
  @RequiresPermissions("apply:list")
  public R list(@RequestParam Map<String, Object> params) {
    //查询列表数据
    Query query = new Query(params);
    List<SysApplyEntity> sysApplyList = sysApplyService.queryList(query);
    int total = sysApplyService.queryTotal(query);
    PageUtils pageUtil = new PageUtils(sysApplyList, total, query.getLimit(), query.getCurrPage());
    return R.ok().put("data", pageUtil);
  }

  /**
   * 查看信息
   */
  @RequestMapping("/info/{id}")
  @RequiresPermissions("apply:info")
  public R info(@PathVariable("id") Long id) {
    SysApplyEntity sysApply = sysApplyService.getById(id);
    return R.ok().put("data", sysApply);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  @RequiresPermissions("apply:save")
  public R save(@RequestBody SysApplyEntity sysApply) {
    ValidatorUtils.validateEntity(sysApply, AddGroup.class);
    List<SysApplyEntity> applyList = sysApplyService.list(
        new QueryWrapper<SysApplyEntity>().eq("dept_id",getUser().getDeptId()).eq("name",sysApply.getName())
    );
    if (applyList.size() > 0) {
      return R.error("应用名不可重复使用");
    }
    Integer keyNum = 13;
    Integer secretNum = 10;
    String appKey = CharUtil.getRandomString(keyNum).toLowerCase();
    String appSecret = CharUtil.getRandomString(secretNum);
    sysApply.setAppKey(appKey);
    sysApply.setAppSecret(appSecret);
    sysApply.setCreateUserId(getUserId());
    sysApply.setCreateTime(new Date());
    sysApply.setDeptId(getUser().getDeptId());
    sysApplyService.save(sysApply);
    return R.ok("执行成功");
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  @RequiresPermissions("apply:update")
  public R update(@RequestBody SysApplyEntity sysApply) {
    ValidatorUtils.validateEntity(sysApply, UpdateGroup.class);
    List<SysApplyEntity> applyList = sysApplyService.list(
        new QueryWrapper<SysApplyEntity>().eq("dept_id",getUser().getDeptId()).eq("name",sysApply.getName())
    );
    if (applyList.size() > 0) {
      return R.error("应用名不可重复使用");
    }
    sysApplyService.updateById(sysApply);
    return R.ok("执行成功");
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  @RequiresPermissions("apply:delete")
  public R delete(@RequestBody Long[] ids) {
    sysApplyService.removeByIds(Arrays.asList(ids));
    return R.ok();
  }
}
